<?php
            
use app\model\Theme; 


/**
 * Desc:领泽奖品关卡宝箱设置
 * Time: 2021/5/27 2:53 下午
 */
class Index_Page extends PluginWebPage {

    function main($method)
    {
        $method = 'lingze_main';
        global $_G,$_P,$_S,$_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $page_index = max(1,intval($_P['page']));
        $page_size = 10;
        $page_start = ($page_index-1)*$page_size;
        $chest_name = isset($_P['chest_name'])?$_P['chest_name']:'';
        $level_sort = isset($_P['level_sort'])?$_P['level_sort']:0;
        $status = isset($_P['status'])?$_P['status']:1;
        $lists = LingzeModel::getPrizeList($themeid,$chest_name,$status,$level_sort,0,0);
        $list = LingzeModel::getPrizeList($themeid,$chest_name,$status,$level_sort,$page_start,$page_size);
        $total = $page_total = count($lists);
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/lingze/prize/main', $return_clicent);
    }

    function add()
    {
        return $this->post();
    }

    function edit()
    {
        return $this->post();
    }

    function delete()
    {
        global $_G,$_P,$_S,$_C;
        $lc_id = trim($_P['lc_id']);
        $themeid = trim($_P['themeid']);
        if ($lc_id && $themeid){
            $res = LingzeModel::deletePrize($lc_id,$themeid);
            if ($res){
                show_json(1,['msg'=>'操作成功']);
            }else{
                show_json(0,['msg'=>'操作失败']);
            }
        }else{
            show_json(0,'活动入口不正确');
        }
    }

    protected function post()
    {
        global $_G, $_P;
        $lc_id = intval($_P['lc_id']);
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        if ($_G['ispost'] && $themeid) {
            $chest_name = trim($_P['chest_name']);
            $box_name = trim($_P['box_name']);
            $level_sort = trim($_P['level_sort']);
            $type = trim($_P['type']);
            $type_num = trim($_P['type_num']);
            $small_img = tomedia($_P['small_img']);
            $has_item = LingzeModel::getPrizeItemByWhere(['type'=>$type,'type_num'=>$type_num,'themeid'=>$themeid]);

            $themeid = $_P['themeid'];
            $status = $_P['status'];
            $data = [
                'chest_name'=>$chest_name,
                'box_name'=>$box_name,
                'level_sort'=>$level_sort,
                'type'=>$type,
                'type_num'=>$type_num,
                'small_img'=>$small_img,
                'themeid'=>$themeid,
                'status'=>1
            ];
            if ($level_sort!=1){
                $first_level = LingzeModel::getPrizeItemBySort(1,$themeid);
                if (!$first_level){
                    show_json(0,['msg'=>'未设置第一关奖品!']);
                    return;
                }
            }
            if (!$lc_id){
//                if ($has_item){
//                    show_json(0,['msg'=>'新增失败！相同操作的奖项，操作次数不可相同']);
//                    return;
//                }
                $res = LingzeModel::insertPrize($data);
            }else{
//                if ($has_item && $has_item['lc_id']!=$lc_id){
//                    show_json(0,['msg'=>'修改失败！相同操作的奖项，操作次数不可相同']);
//                    return;
//                }
                $where = ['lc_id'=>$lc_id,'themeid'=>$themeid];
                $res = LingzeModel::editPrizeItemByWhere($where,$data);
            }
            if (!$res){
                show_json(0,['msg'=>'操作失败']);
            }else{
                show_json(1,['msg'=>'操作成功']);
            }
        }
        if ($lc_id && $themeid) {
            $item = LingzeModel::getPrizeItemById($lc_id,$themeid);
        }
        $return_clicent = get_defined_vars();
return $this->view('/lingze/prize/edit', $return_clicent);
    }
}